//var REG_EMAIL = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
var spiderDanMu=function(){
	var index = 1;
	var flag = false;
	/*if(!emailValidate($("#email").val()))
		return;*/
	$('.subDiv').each(function(){
		var tvname = $(this).find('.tvname').val();
		var director = $(this).find('.director').val();
		var actor = $(this).find('.actor').val();
		if(!tvname || !director || !actor){
			flag = true;
			index= $(this).index()+1;
		}
	});
	if(flag == true){
		alert("请完善第"+index+"行信息");
		return;
	}
	var IQYList = [];
	var QQList = [];
	var YouKuList = [];
	var MGList = [];
	var IQYMovieList = [];
	var QQMovieList = [];
	var YouKuMovieList = [];
	var MGMovieList = [];
	var IQYParam = {};
	var QQParam = {};
	var YouKuParam = {};
	var MGParam = {};
	var IQYMovieParam = {};
	var QQMovieParam = {};
	var YouKuMovieParam = {};
	var MGMovieParam = {};
	$('.platform').each(function(){
		var param = {};
		var targetObj = {};
		var IQYNum = 0;
		var QQNum = 0;
		var YouKuNum = 0;
		var MGNum = 0;
		var IQYMovieNum = 0;
		var QQMovieNum = 0;
		var YouKuMovieNum = 0;
		var MGMovieNum = 0;
		var tvname = $(this).parents('.subDiv').find('.tvname').val();
		var director = $(this).parents('.subDiv').find('.director').val();
		var actor = $(this).parents('.subDiv').find('.actor').val();
		var tvtype = $(this).parents('.subDiv').find('.tvtype').val();
		var platform = $(this).val();
		targetObj.tvName = tvname;
		targetObj.director = director;
		targetObj.actor = actor;
		targetObj.tvType = tvtype;
		param.thread=1;
		param.pipeline='ResDanMu';
		param.dsKey='emptyDS';
		param.sleepTime=2;
		param.randomSleep=2;
		param.siteReqHeader=1;
		/*param.email=$("#email").val();
		param.subject="弹幕数据爬取";
		param.content="csv文件格式：剧名|用户ID|用户名|会员等级|时间|点赞数|平台";*/
		if(tvtype == '电视剧'){
			if(platform == 'IQYDanMu'){
				IQYNum++;
				IQYList.push(targetObj);
				if(IQYNum == 1){
					param.pageProcessor=platform;
					IQYParam = param;
	        	}
			}
			if(platform == 'QQDanMu'){
				QQNum++;
				QQList.push(targetObj);
				if(QQNum == 1){
					param.pageProcessor=platform;
					QQParam = param;
	        	}
			}
			if(platform == 'YouKuDanMu'){
				YouKuNum++;
				YouKuList.push(targetObj);
				if(YouKuNum == 1){
					param.pageProcessor=platform;
					YouKuParam = param;
	        	}
			}
			if(platform == 'MGDanMu'){
				MGNum++;
				MGList.push(targetObj);
				if(MGNum == 1){
					param.pageProcessor=platform;
					MGParam = param;
	        	}
			}
		}
		else if(tvtype == '电影'){
			if(platform == 'IQYDanMu'){
				IQYMovieNum++;
				IQYMovieList.push(targetObj);
				if(IQYMovieNum == 1){
					param.pageProcessor='IQYMovieDanMu';
					IQYMovieParam = param;
	        	}
			}
			if(platform == 'QQDanMu'){
				QQMovieNum++;
				QQMovieList.push(targetObj);
				if(QQMovieNum == 1){
					param.pageProcessor='QQMovieDanMu';
					QQMovieParam = param;
	        	}
			}
			if(platform == 'YouKuDanMu'){
				YouKuMovieNum++;
				YouKuMovieList.push(targetObj);
				if(YouKuMovieNum == 1){
					param.pageProcessor='YouKuMovieDanMu';
					YouKuMovieParam = param;
	        	}
			}
			if(platform == 'MGDanMu'){
				MGMovieNum++;
				MGMovieList.push(targetObj);
				if(MGMovieNum == 1){
					param.pageProcessor='MGMovieDanMu';
					MGMovieParam = param;
	        	}
			}
		}
	});
		alert("任务启动成功");
	if(!$.isEmptyObject(IQYParam)){
		runSpider({targetList:IQYList,paraMap:IQYParam});
	}
	if(!$.isEmptyObject(QQParam)){
		runSpider({targetList:QQList,paraMap:QQParam});
	}
	if(!$.isEmptyObject(YouKuParam)){
		runSpider({targetList:YouKuList,paraMap:YouKuParam});
	}
	if(!$.isEmptyObject(MGParam)){
		runSpider({targetList:MGList,paraMap:MGParam});
	}
	if(!$.isEmptyObject(IQYMovieParam)){
		runSpider({targetList:IQYMovieList,paraMap:IQYMovieParam});
	}
	if(!$.isEmptyObject(QQMovieParam)){
		runSpider({targetList:QQMovieList,paraMap:QQMovieParam});
	}
	if(!$.isEmptyObject(YouKuMovieParam)){
		runSpider({targetList:YouKuMovieList,paraMap:YouKuMovieParam});
	}
	if(!$.isEmptyObject(MGMovieParam)){
		runSpider({targetList:MGMovieList,paraMap:MGMovieParam});
	}
};

function runSpider(para)
{
	$.ajax({  
	    type: "POST",  
	    url: "/Spider-WebMagic/spiderDanMuRunner/runSpiderDanMuAndSend",  
	    //url: "/spiderDanMuRunner/runSpiderDanMuAndSend",  
	    data: JSON.stringify(para),//将对象序列化成JSON字符串  
	    dataType:"text",  
	    contentType : 'application/json;charset=utf-8', //设置请求头信息  
	    success: function(data)
	    {  
	        if(data=="1")
	        	alert("任务启动失败");
	    } 
	 });
}

function addRow(own){
	$(own).before('<div class="subDiv"><span>剧名：<input class="inputStyle tvname" type="text"></span><span>导演：<input class="inputStyle director" type="text"></span><span>主演：<input class="inputStyle actor" style="width:300px;" type="text"></span><span>平台：<select class="inputStyle platform" style="width: 100px;height: 30px;"><option value="IQYDanMu">爱奇艺</option><option value="QQDanMu">腾讯</option><option value="YouKuDanMu">优酷</option><option value="MGDanMu">芒果</option></select></span><span>剧类型：<select class="inputStyle tvtype" style="width: 100px;height: 30px;"><option>电视剧</option><option>电影</option></select></span><span style="cursor:pointer;" onclick="removeRow(this)">x</span></div>');
}

function removeRow(own){
	$(own).parent().remove();
}

$(function()
{   
	$('#danmuBtn').click(spiderDanMu);
});

/*var emailValidate=function(eml)
{
	if(REG_EMAIL.test(eml))
		return true;
	else
	{
		alert('邮箱格式错误');
		return false;
	}
};*/